package com.hoangkien.activity;

import com.capricorn.ArcMenu;
import com.capricorn.RayMenu;
import com.hoangkien.coloring.ColorGFX;
import com.hoangkien.coloring.ColorGfxData;
import com.hoangkien.coloring.Node;
import com.mango.hoangkien.coloringkids.R;
import com.mango.hoangkien.coloringkids.R.layout;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ColoringBitmapActivity extends Activity {
	private static final int[] ITEM_DRAWABLES = { R.drawable.image, R.drawable.palm,
		R.drawable.container, R.drawable.eraser };
	
	private FrameLayout mFlColorBody;
	public ColorGFX colorGFX;
	public ProgressBar pbFloodFill;
	private int sCurrentImageId = 0;
    private int mMinImageId;
    private int mMaxImageId;
    private Node[] mNodeData;
    private boolean isSavedState = false;
    private ColorGfxData savedData = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_coloring_bitmap);
		// TODO: Arcmenu
		RayMenu rayMenu = (RayMenu) findViewById(R.id.ray_menu);
		final int itemCount = ITEM_DRAWABLES.length;
		for (int i = 0; i < itemCount; i++) {
			ImageView item = new ImageView(this);
			item.setImageResource(ITEM_DRAWABLES[i]);
			final int position = i;
			rayMenu.addItem(item, new OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(ColoringBitmapActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
				}
			});
		}
		
		// TODO: coloring
		mFlColorBody = (FrameLayout) findViewById(R.id.flColorBody);
		mFlColorBody.requestFocus();
	}
	
	private void loadColorCanvas() {
        loadImage();
        colorGFX.setTag(0);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(colorGFX.imageWidth, colorGFX.imageHeight);
        params.gravity = Gravity.CENTER;
        colorGFX.setLayoutParams(params);
        mFlColorBody.addView(colorGFX);
        mFlColorBody.addView(pbFloodFill);
    }
	
	private void loadImage() {

        if (sCurrentImageId < mMinImageId) {
            sCurrentImageId = mMaxImageId;
        }
        if (sCurrentImageId > mMaxImageId) {
            sCurrentImageId = mMinImageId;
        }
        Node node = mNodeData[sCurrentImageId];
        String mResourceName = node.body;
        String mResourcePaint = mResourceName + "_map";
        int resId = getResources().getIdentifier(mResourceName, "drawable",  getPackageName());
        Bitmap picture = decodeImage(resId);
        if (colorGFX == null) {
            if (isSavedState && savedData != null) {
                colorGFX = new ColorGFX(this, picture.getWidth(), picture.getHeight(), isSavedState, savedData.bitmap);
            }
            else {
                colorGFX = new ColorGFX(this, picture.getWidth(), picture.getHeight());
            }
        }
        else {
            if (colorGFX.pathCanvas != null) {
                colorGFX.clear();
            }
        }
        colorGFX.isNextImage = true;
        colorGFX.pictureBitmapBuffer = picture;
        colorGFX.paintBitmapName = mResourcePaint;
    }
	
	private Bitmap decodeImage(int resId) {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float screenWidth = dm.widthPixels;
        float screenHeight = dm.heightPixels;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), resId, options);

        int inSampleSize = 1;
        int imageWidth = options.outWidth;
        int imageHeight = options.outHeight;
        boolean scaleFailed = false;
        Bitmap scaledBitmap = null;
        float resizeRatioHeight = 1;
        if (imageWidth > screenWidth || imageHeight > screenHeight) {
            resizeRatioHeight = (float) imageHeight / (float) screenHeight;
            inSampleSize = (int) resizeRatioHeight;

            if (inSampleSize <= 1) {
                scaleFailed = true;
            }
        }
        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;

        Bitmap picture = BitmapFactory.decodeResource(getResources(), resId, options);
        if (scaleFailed) {
            int newWidth = (int) (picture.getWidth() / resizeRatioHeight);
            int newHeight = (int) (picture.getHeight() / resizeRatioHeight);
            scaledBitmap = Bitmap.createScaledBitmap(picture, newWidth, newHeight, true);
            picture.recycle();
        }
        else {
            scaledBitmap = picture;
        }

        return scaledBitmap;
    }
	
	

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		HomekidsActivity.actionLaunch(ColoringBitmapActivity.this);
	}
	
}
